package com.ghh.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ghh.constant.MessageConstant;
import com.ghh.dao.SetmealDao;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.entity.Result;
import com.ghh.pojo.Setmeal;
import com.ghh.service.SetmealService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    //根据id查套餐基本信息
    @Override
    public Setmeal findSetmealById(Integer id) {
        return setmealDao.findSetmealById(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {

        return setmealDao.findSetmealCount();
    }

    //添加套餐
    @Override
    public void addSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {
        //新增套餐，返回id
        setmealDao.addSetmeal(setmeal);
        Integer setmealId= setmeal.getId();
        //调用添加关联表方法
        this.addSetmealAndCheckGroup(setmealId,checkgroupIds);

        //添加新套餐时重构套餐列表的静态页面
        generteSetmealListHtml();
        //为新添加的套餐生成静态页面
        generteSetmealDetailHtml(setmealId);
    }

    //分页查询套餐
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //定义需要的数据
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        //分页插件，会在执行sql之前将分页关键字追加到SQL后面
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //获取所有套餐图片的名字
    @Override
    public Set<String> findAllFileName() {
        return setmealDao.findAllFileName();
    }

    //查询所有套餐信息
    @Override
    public List<Setmeal> getSetmeal() {
        List<Setmeal> list = setmealDao.findAllSetmeal();

       /* //生成套餐列表的静态页面
        generteSetmealListHtml();
        //生成所有套餐详情的静态页面
        for (Setmeal setmeal : list) {
            generteSetmealDetailHtml(setmeal.getId());
        }*/

        return list;
    }

    //根据套餐ID查询套餐及检查项与检查组的详情
    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    //将套餐与检查组的关联添加进关联表
    public void addSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds){
        //循环检查项ID，添加进关联表
        if (checkgroupIds!=null&&checkgroupIds.length>0) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.addSetmealAndCheckGroup(setmealId, checkgroupId);
            }
        }
    }


    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    private String outputpath = "E:/Project/projectOne/projectOne-health/health-mobile/src/main/webapp/pages";

    //生成套餐列表的静态页面
    public void generteSetmealListHtml(){
        try {
            //获得配置对象
            Configuration configuration = freeMarkerConfigurer.getConfiguration();
            Template template = configuration.getTemplate("mobile_setmeal.ftl");
            //构造输出流
            Writer out = new FileWriter(new File(outputpath + "/setmeal_detail.html"));
            //输出文件
            Map map = new HashMap();
            map.put("setmealList",setmealDao.findAllSetmeal());
            template.process(map,out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //生成单个套餐详情的静态页面
    public void generteSetmealDetailHtml(Integer setmealId){
        try {
            //获得配置对象
            Configuration configuration = freeMarkerConfigurer.getConfiguration();
            Template template = configuration.getTemplate("mobile_setmeal_detail.ftl");
            //构造输出流
            Writer out = new FileWriter(new File(outputpath + "/setmeal_detail_"+setmealId+".html"));
            //输出文件
            Map map = new HashMap();
            map.put("setmeal",setmealDao.findById(setmealId));
            template.process(map,out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
